package com.guoguo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Images;
import com.guoguo.entity.vo.AcAlbumImagesVO;
import com.guoguo.entity.vo.AlbumImagesVO;
import com.guoguo.entity.vo.CommentVO;
import com.guoguo.entity.vo.FavoriteVO;
import com.guoguo.entity.vo.SysImgVO;
import com.guoguo.mapper.CommentMapper;
import com.guoguo.mapper.FavoriteMapper;
import com.guoguo.mapper.ImagesMapper;
import com.guoguo.mapper.SysImgMapper;
import com.guoguo.service.IImagesService;
import com.guoguo.utils.AliOssUtils;

/**
 * Created by Administrator on 2017/8/10 0010.
 */
@Service
public class ImagesServiceImpl implements IImagesService {

    //ImagesMapper接口
    @Autowired
    private ImagesMapper imagesMapper;

    //ImagesMapper接口
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private SysImgMapper sysImgMapper;

    @Override
	public PageBean<AlbumImagesVO> getAlbumImageList(String albId) {
        return getAlbumImagesList(albId, 1, 200);
    }

    @Override
	public PageBean<AlbumImagesVO> getAlbumImagesList(String albId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AlbumImagesVO> albumImagesVOs = imagesMapper.getAlbumImageList(albId);
//        List<AliOssImageConfig> aliOssImageConfigsList = new ArrayList<>();
//        aliOssImageConfigsList.add(new AliOssImageConfig("test"));

        for (AlbumImagesVO albumImagesVO : albumImagesVOs) {
            List<String> files = new ArrayList<>();
            files.add(albumImagesVO.getOriginalUrl());
            files.add(albumImagesVO.getCompressionUrl());
            files.add(albumImagesVO.getThumbnailsUrl());
            files.add(albumImagesVO.getModelIcon());
            Map<String, String> urlMap = AliOssUtils.getImageUrlList(files);//, aliOssImageConfigsList);

            albumImagesVO.setOriginalUrl(urlMap.get(albumImagesVO.getOriginalUrl()));
            albumImagesVO.setCompressionUrl(urlMap.get(albumImagesVO.getCompressionUrl()));
            albumImagesVO.setThumbnailsUrl(urlMap.get(albumImagesVO.getThumbnailsUrl()));
            albumImagesVO.setModelIcon(urlMap.get(albumImagesVO.getModelIcon()));
        }
        return new PageBean<AlbumImagesVO>(albumImagesVOs);
    }

    @Override
    public PageBean<AcAlbumImagesVO> getImagesById(String albId) {
        List<AcAlbumImagesVO> acAlbumImagesVOs = imagesMapper.getImagesById(albId);

        return new PageBean<AcAlbumImagesVO>(acAlbumImagesVOs);
    }

    //获取图片评论
    @Override
	public PageBean<CommentVO> getCommentList(String imgId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CommentVO> list = commentMapper.getCommentList(imgId);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (CommentVO commentVO : list) {
            commentVO.setIcon(AliOssUtils.getImageUrl(commentVO.getIcon()));//, aliOssImageConfig));
        }
        return new PageBean<CommentVO>(list);

    }

    @Override
    public PageBean<CommentVO> getHotCommentList(String imgId, int pageNum, int pageSize) {
        PageHelper.startPage(1, 3);
        List<CommentVO> list = commentMapper.getHotCommentList(imgId);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (CommentVO commentVO : list) {
            commentVO.setIcon(AliOssUtils.getImageUrl(commentVO.getIcon()));//, aliOssImageConfig));
        }
        return new PageBean<CommentVO>(list);
    }

    //获取收藏的图片列表
    @Override
    public PageBean<FavoriteVO> getImageList(String userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<FavoriteVO> imagesVOs = favoriteMapper.getImageList(userId, pageNum, pageSize);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (FavoriteVO imagesVO : imagesVOs) {
            if (imagesVO.getThumbnailsUrl() != null || !"".equals(imagesVO.getThumbnailsUrl())) {
                imagesVO.setThumbnailsUrl(AliOssUtils.getImageUrl(imagesVO.getThumbnailsUrl()));//, aliOssImageConfig));
            }
            if (imagesVO.getModelIcon() != null || !"".equals(imagesVO.getModelIcon())) {
                imagesVO.setModelIcon(AliOssUtils.getImageUrl(imagesVO.getModelIcon()));//, aliOssImageConfig));
            }
        }
        return new PageBean<FavoriteVO>(imagesVOs);
    }

    /**
     * 获取系统图片（轮播图片和启动页图片）
     *
     * @return
     */
    @Override
	public PageBean<SysImgVO> getImages() {
        PageHelper.startPage(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        List<SysImgVO> list = sysImgMapper.getSysImages();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (SysImgVO sysImgVO : list) {
            String url = AliOssUtils.getImageUrl(sysImgVO.getSysImgUrl());//, aliOssImageConfig);
            sysImgVO.setSysImgUrl(url);
        }
        return new PageBean<SysImgVO>(list);

    }

    @Override
    public List<Images> selectBy(Images images) {
        return imagesMapper.selectBy(images);
    }

    @Override
    public int insert(Images images) {
        return imagesMapper.insert(images);
    }

    @Override
    public int insertSelective(Images images) {

        return imagesMapper.insertSelective(images);
    }

    @Override
    public int countBy(Images images) {
        return imagesMapper.countBy(images);
    }

    @Override
    public Images selectByPrimaryKey(String imgId) {
        return imagesMapper.selectByPrimaryKey(imgId);
    }

    @Override
    public int deleteByPrimaryKey(String imgId) {
        return imagesMapper.deleteByPrimaryKey(imgId);
    }

    @Override
    public int deleteBy(Images images) {
        return imagesMapper.deleteBy(images);
    }

    @Override
    public int updateBySelective(Images images) {
        return imagesMapper.updateBySelective(images);
    }

    @Override
    public int updateBy(Images images) {
        return imagesMapper.updateBy(images);
    }

    @Override
    public int updateByPrimaryKeySelective(Images images) {
        return imagesMapper.updateByPrimaryKeySelective(images);
    }

    @Override
    public int updateByPrimaryKey(String imgId) {
        return imagesMapper.updateByPrimaryKey(imgId);
    }
}
